<?php

function list_jabatan_man_power(){
	
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'No Org '=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:45%'),  
		'Jab. terkait'=>array('style'=>'text-align:center;border-bottom:2px solid;width:22%'),  
		'Shift'=>array('style'=>'border-bottom:2px solid;width:5%') ,
		'Qty'=>array('style'=>'border-bottom:2px solid;width:5%') ,
		' '=>array('style'=>'border-bottom:2px solid;width:6%'), 
	);
	
	$com = isset($_GET['com']) ? $_GET['com']: '';
	$task = isset($_GET['task']) ? $_GET['task']: '';
	$field = isset($_GET['field']) ? $_GET['field']: '';
	$key = isset($_GET['key']) ? $_GET['key']: '';
	if(isset($_GET['key'])){
		$query = "
			SELECT a.* 
			FROM jabatan a	
			INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id 
			OR a.nomor_posisi ='{$_GET['key']}' 
			AND b.active='Y' 
			ORDER BY a.urut
		";
	}else{
	$query = "SELECT a.* 
			FROM jabatan a	
			INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id 
			WHERE b.active='Y' 
			ORDER BY a.urut" ;
	}
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =20;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$com}&task={$task}&field={$field}&key={$key}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir '; 
	$pager_url_last =null;
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,
				$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page);  
	
	$i = ($current_page  - 1 ) * 20; 
	$row = array();

	while($ey = my_fetch_array($result) ){
		$i++;
		$jenis_shift_array = array(
		'1' => 'Reguler',		
		'2' => 'Shift',
		'3' => 'Fleksibel',
		);
		$shiDat = explode("," ,$ey['kebutuhan_tenaga_kerja']);
		$tombol_aktif=true;
		$jabatan_terkait= jumlah_jabatan_terkait($ey['jabatan_id'] ) ;
		 
		if($tombol_aktif){
			$editproperty = array(
				'href'=>'index.php?com=jabatan_man_power&task=edit&id='.$ey['jabatan_id'] ,
				'rel'=>'facebox',
				'title'=>'Edit'
			);
			$edit_button = button_icon( 'b_edit.png' , $editproperty  );			
			$jabatanterkaitproperty = array(
				'href'=>'index.php?com=jabatan_man_power&task=list_jabatan_terkait&id='.$ey['jabatan_id'] ,
				'title'=>'Jabatan Terkait'
			);	
			$jabatanterkait_button = button_icon( 'b_props.png' , $jabatanterkaitproperty  );			
			$jenis_shift=$jenis_shift_array[ $ey['shift'] ] ;
		}else{
			$edit_button = button_icon( 'b_edit_off.png' , ''  );	
			$jabatanterkait_button =button_icon( 'bd_deltbl.png' , ''  );		
			$shiDat[0]=$shiDat[1]=$shiDat[2]='';
			$jenis_shift='' ;
		}
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nomor'=>  $ey['nomor_posisi'],
			'jabatan'=>  $ey['nama_jabatan'] ,
			'jt'=>  position_text_align($jabatan_terkait ,	'center'), 
			'shift'=> $jenis_shift, 	
			'jumlah'=> position_text_align (str_replace(",","",$ey['kebutuhan_tenaga_kerja']),'center'), 	
			'operasi'=> position_text_align(  $edit_button.' '.$jabatanterkait_button , 'right'),  
		);
		
	}
	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" /><span style="font:10px verdana"><b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" /><input class="simple_search" type="submit" value=" CARI " /> </form>';
	$menuontop = array(
	//	'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com=report&task=jabatan_existing\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');')
	);
	$printmenu = additional_menu_on_list( $menuontop ,$search ); 
	return $printmenu . table_builder($header , $datas ,  7 , false ,$paging  ) ;
}


function jumlah_jabatan_terkait($jabatan_id){
	$query = "SELECT * FROM jabatan_terkait_link WHERE 	parent_jabatan_id = {$jabatan_id}";
	$result = my_query($query);
	return my_num_rows($result);
}

function form_jabatan_man_power_create($jabatan_id){
	my_set_file_js(array( 
		'components/system/jquery/combomulti/jquery.chainedSelects.js' 
	)); 

	$jabatan = loaddata_jabatan($jabatan_id) ;
	$shiDat = explode("," ,$jabatan['kebutuhan_tenaga_kerja']);
	$form_shif = '<input type="text" name="sh1" size="5" value="'.$shiDat[0].'" />   &nbsp; <input type="text" name="sh2"  size="5"  value="'.$shiDat[1].'" /> &nbsp; <input type="text" name="sh3"  size="5"  value="'.$shiDat[2].'" /> &nbsp;';
	$form_non_shif = '<input type="text" name="sh2"  style="width:40px;"  value="'.$shiDat[1].'" /> ';
	
	my_set_code_js('  
		$(document).ready(function() { 
			$(\'#shift\').change(function(){   
				if ($(this).val()==\'1\' ) {
					$("#ashift").replaceWith("<span id=\'ashift\'>'. addslashes($form_non_shif).'</span>");
				}else if ($(this).val()==\'2\' ) {  
					$("#ashift").replaceWith("<span id=\'ashift\'>'. addslashes($form_shif)  .'</span>");
				}else if ($(this).val()==\'3\' ) {  
					$("#ashift").replaceWith("<span id=\'ashift\'>'. addslashes($form_non_shif).'</span>");
				}else{
					$("#ashift").replaceWith("<span id=\'ashift\'></span>");
				}		
			}); 
		}) ;
	' );
	my_set_code_css('
	 
	#bshift{
		display:none;
	}
	#didikloading
		{  
			 
			background:#ff0000;
			color:#fff;
			font-size:14px;
			font-familly:Arial;
			padding:2px; 
			display:none;
			float:left;
		}
	');
		
	$view = form_header( "jabatan_man_power" , "jabatan_man_power"  );
	$view .= form_field_display( '<span class="label_form">'.$jabatan['nomor_posisi'] .'</span>'  , "<b>Nomor Posisi</b>"    );  	
	$view .= form_field_display( '<span class="label_form">'.$jabatan['nama_jabatan'].'</span>'   , "<b>Jabatan</b>"    );  
	$view .= form_field_display( '<span class="label_form">'.$jabatan['departemen'] .'</span>'  , "<b>Departemen</b>"    ); 
	$shift_datas = array(
		'1' => 'Reguler',
		'2' => 'Shift',
		'3' => 'Fleksibel',
	);
	$shift = array(
		'name'=>'shift',
		'value'=>( isset($_POST['shift']) ? $_POST['shift'] : $jabatan['shift'] ),
		'id'=>'shift', 
	);
	$form_field_shift = form_dropdown($shift , $shift_datas );
	$view .= form_field_display( $form_field_shift  , "Shift"   );
	
	$view .= form_field_display(  '<span id="ashift"> '.$form_non_shif .' Orang </span>' , "Kebutuhan tenaga kerja"   ); 	
	
	$submit = array(
		'value' => (  '  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	
	$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );
	$view .= form_footer( );	
	return $view;
}

function form_jabatan_man_power_submit($jabatan_id = 0){
	$shift=$_POST['shift'] ;
	if($shift='2'){
		$kebutuhan = $_POST['sh1'] .",". (isset($_POST['sh2']) ? $_POST['sh2'].",".$_POST['sh3'] : ""	);
	}else{
		$kebutuhan = (isset($_POST['sh2']) ? "0,".$_POST['sh2'].",0" : ""	);
	}
	$datas = array(
		'shift'=>my_type_data_str( $_POST['shift'] ),
		'kebutuhan_tenaga_kerja'=>my_type_data_str( $kebutuhan ),			
	);
	return my_update_record('jabatan','jabatan_id',$jabatan_id, $datas);
}

function form_jabatan_man_power_validate($jabatan_id){
	$errsubmit = false;
	$err = array();
	if( $_POST['shift'] == '0'){
		$errsubmit = true;
		$err[] = "Shift belum di isi";
	}
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}
/*
==========================
	JABATAN TERKAIT
==========================	
*/
function list_jabatan_terkait($jabatan_id){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=jabatan_terkait_delete&id=\'+id;
			}
			return false;
		}		
	');	
 
	$header  = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'), 
		'Nomor Organisasi'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:65%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'),   
	);

	$query = "SELECT a.jabatan_terkait_id,b.* FROM jabatan_terkait_link a 
		INNER JOIN jabatan b ON b.jabatan_id=a.jabatan_id 
		WHERE a.parent_jabatan_id={$jabatan_id} AND a.jabatan_id<> {$jabatan_id}" ;
		
	$result = my_query($query);
	$i = 0;
	$row = array();
	while($ey = my_fetch_array($result) ){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=jabatan_man_power&task=jabatan_terkait_create&id='.$jabatan_id.'&jt_id='.$ey['jabatan_terkait_id'] ,
			'rel'=>'facebox',
			'title'=>'Edit'
			);	
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );		
		
		$deleteproperty = array(
			'href'=>'javascript:void();', 
			'title'=>'Delete',
			'onclick'=>'javascript:confirmDelete( '.$ey['jabatan_terkait_id'].')'
			);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );

		$row[] = array(
			'#'=>position_text_align ($i, 'center'),    
			'no'=>  $ey['nomor_posisi'],    
			'tj'=>  $ey['nama_jabatan'] ,     
			'operasi'=> position_text_align( $edit_button .' '.$delete_button , 'right'),
		);
	}
	$datas = table_rows($row);

	$jabatan = loaddata_jabatan($jabatan_id) ;
	$view  = form_header( "..." , "..."  );
	$view .= '<tr><td><td><div style="width:100%;text-align:right;">
	<input type="button" value="Tambah Data" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com=jabatan_man_power&task=jabatan_terkait_create&id='.$jabatan_id.'&jt_id=0\' });" class="main_button" /> 
	<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?com=jabatan_man_power\'"/>
	</div></td></tr>'   ; 
	$view .= form_field_display( '<span class="label_form">'.$jabatan['nomor_posisi'] .'</span>'  , "<b>Nomor Posisi</b>"    );  	
	$view .= form_field_display( '<span class="label_form">'.$jabatan['nama_jabatan'].'</span>'   , "<b>Jabatan</b>"    );  
	$view .= form_field_display( '<span class="label_form">'.$jabatan['departemen'] .'</span>'  , "<b>Departemen</b>"    ); 
	return  $view. table_builder($header , $datas ,  4, false );
}

function form_jabatan_terkait_create($id, $jt_id=0){
	my_set_file_js(
		array(
		'components/system/jquery/autocomplete/jquery.autocomplete.js'  
		)
	);
	my_set_file_css(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.css' 
		)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

	function selectItem(li) {
		findValue(li);
	}

	function formatItem(row) {
		return   row[0] ;
		}
 
	$(document).ready(function() {
		$("#nama_jabatan").autocomplete(
			"autocomplete_daftar_jabatan.php",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:5,
				onItemSelect:selectItem,
				onFindValue:findValue,
				formatItem:formatItem,
				autoFill:true
			}
		);
	});

	function checkNameJabatan(name){
		$.get("check_jabatan_names.php", { nama: name  },
		function(data){
			if(data ==  \'0\' ){
				alert(\'Jabatan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
				$(\'#nama_jabatan\').val(\'\');
				return false;
			} 
		});
	}
	');

	
	$view = form_header( "..." , "..."  );
	if($jt_id>0){
		$fields = my_get_data_by_id('jabatan_terkait_link','jabatan_terkait_id',$jt_id);
		$jabatan_id=$fields['jabatan_id'];
		$fields = my_get_data_by_id('jabatan','jabatan_id',$jabatan_id);
		$nomor_organisasi=$fields['nomor_posisi']."/".$fields['nama_jabatan'] ;
	}else{
		$nomor_organisasi='';
	}	
	$jabatan = array(
			'name'=>'nama_jabatan',
			'value'=>(isset($_POST['nama_jabatan'])? $_POST['nama_jabatan'] : $nomor_organisasi  ),
			'id'=>'nama_jabatan',
			'type'=>'textfield',
			'size'=>'50',
			'onchange'=>'javascript:checkNameJabatan(this.value)'
		);
	$form_jabatan = form_dynamic($jabatan);
	$view .= form_field_display( $form_jabatan  , "Nomor (Jabatan) "  );;

	$submit = array(
		'value' => ( $jt_id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	
	$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );
	$view .= form_footer( );	
	return $view;
}

function form_jabatan_terkait_submit($id, $jt_id= 0){
	$jabatan_id = check_jabatan_by_no_org($_POST['nama_jabatan']	);
	$datas = array(
		'jabatan_id'=>my_type_data_int($jabatan_id ),		 	 	 	 	 	 	 
		'parent_jabatan_id'=>my_type_data_int($id), 
	);

	if($jt_id > 0){
		return my_update_record('jabatan_terkait_link','jabatan_terkait_id',$jt_id, $datas);	
	}
	return my_insert_record('jabatan_terkait_link' , $datas);
}

function form_jabatan_terkait_validate($id, $jt_id= 0){
	$errsubmit = false;
	$err = array();
	$jabatan=$_POST['nama_jabatan'];
	$jabatan_id = check_jabatan_by_no_org($jabatan);
	$fields = my_get_data_by_id('jabatan','jabatan_id',$id);
	$jabatan_terkait = my_get_data_by_id('jabatan','jabatan_id',$jabatan_id);
	if( $jabatan == ''){
		$errsubmit = true;
		$err[] = "Jabatan belum di isi";
	}	
	if($jabatan_id==$id){
		$errsubmit = true;
		$err[] = "Jabatan Terkait tidak boleh sama dengan jabatan";
	}	
	/*
	if($fields['parent_struktur_id']<>$jabatan_terkait['parent_struktur_id']){
		$errsubmit = true;
		$err[] = "Parent tidak sama";
	}*/
	$query="SELECT jabatan_terkait_id FROM jabatan_terkait_link
		WHERE jabatan_id=".$jabatan_id. 
		" AND jabatan_terkait_id<>".$jt_id; 
		
		
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Jabatan sudah digunakan";	
	}

	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_jabatan_terkait($id){
	
	$query = "DELETE FROM jabatan_terkait_link WHERE jabatan_terkait_id ={$id} ";
	return my_query($query);
}

